<?php
/**
 * 后台公共函数
 */

/**
 * 获取图片路径
 * @param $id
 * @param string $field
 * @return bool|string
 */
function getImgPath($images_id){
    $info = M('picture')->where(array('id'=>$images_id))->find();
    $path = $info ? __ROOT__ . '/' . $info['path'] : false;
    return $path;
}

/**
 * 内容输出时,将内容中的base64_images转换为base64
 * @param $content  文章内容
 * @param $images_id 图片id
 * @return mixed|string
 */
function content_images($content, $images_id){
    $pattern = '/base64_images/U';
    preg_match($pattern,$content,$res);
    if($res){
        if($images_id) {
            $ids = unserialize($images_id);
            foreach ($ids as $key => $value) {
                $img_path = getImgPath($value);
                $content = preg_replace($pattern, $img_path, $content, 1);
            }
        }
    }
    return $content;
}


/**
 * 内容转换，将base64,转换为简化的base64_images,进行内容与图片分离
 * @param $content
 * @return mixed
 */
function content_conversion($content){
    $pattern = '/<img src="(.*)"/U';
    preg_match_all($pattern, $content, $res);
    if(!$res[1]) {
        $data['content'] = $content;
        $data['images_id'] = '';
        return $data;
    }
    foreach ($res[1] as $key => $value) {
        $images[] = base64_to_imgid($value);
    }
    foreach ($images as $key => $value) {
        $content = preg_replace('/data:.*"/U', 'base64_images"', $content, 1);
    }
    $data['content'] = $content;            //文章内容
    $data['images_id'] = serialize($images);//图片id序列化
    return $data;
}

/**
 * base64转换为图片存储,图片路径存储在picture数据表。
 * @param $base64
 * @return int|mixed|string 返回图片id
 */
function base64_to_imgid($base64){
    //对base64进行正则匹配
    preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result);

    //图片格式
    $type = $result[2];

    //实现判断图片是否上传过
    $pattern = '/data:.*base64/U';
    //获取base64的数据
    $res = preg_replace($pattern, '', $base64);
    $str = base64_decode($res);
    $md5 = md5($str);
    $sha1 = sha1($str);
    $res = M('picture')->where(['md5'=>$md5,'sha1'=>$sha1])->find();
    if ($res) {
        //如果图片存在，则返回图片id
        return $res['id'];
    }else{
        //存储目录
        $PATH = 'Uploads/picture/'. date('Y-m-d', time()) . "/";

        //判断目录是否存在，不存在则创建
        if (!is_dir($PATH)) {
            mkdir($PATH, 0777, true);
        }

        //文件名
        $name = md5(rand(100, 999) . time());

        //图片路径
        $imgPath = $PATH . $name . '.' . $type;

        //拼装数据
        $data = [
            'path'        => $imgPath,
            'md5'         => $md5,
            'sha1'        => $sha1,
            'create_time' => time(),
        ];

        //插入数据到数据表
        $id = M('picture')->add($data);

        //返回图片id
        return $id;
    }
}

/**
 * 获取分类树(无限级分类)
 * @param $arr
 * @param int $pid 父级id
 * @param int $level 级别
 * @return array
 */
function getTree($arr,$pid=0,$level=0){
    static $list = array();
    foreach($arr as $value){
        if($value['pid'] == $pid){
            if($level == 0){
                $value['level'] = '';
            }else{
                $value['level'] = $level == 1 ? str_repeat('|&nbsp;---&nbsp;',$level) : '|'.str_repeat('&nbsp;---&nbsp;',$level);
            }
            $list[] = $value;
            getTree($arr,$value['id'],$level+1);
        }
    }
    return $list;
}

/**
 * 获取图片的地址
 * @param $img_id @图片id
 * @return string
 */
function getImage($img_id){
    $res = M('Picture')->where(['id'=>$img_id])->find();
    $url = __ROOT__.'/'.$res['path'];
    return $url;
}

/**
 * 格式化字节大小
 * @param  number $size      字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function format_bytes($size, $delimiter = '') {
    $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
    return round($size, 2) . $delimiter ."&nbsp;".$units[$i];
}

/*
 * 返回格式化时间
 * */
function show_time($time){
    return date("Y-m-d H:i:s",$time);
}

/*
 * 返回固定长度文字超出用...代替
 * */
function recontent($content){
    $len=mb_strlen($content,'utf-8');
    if($len>=25){
        $a=mb_substr($content,0,25,'utf-8')."...";
        return $a;
    }else{
        return $content;
    }
}

//$dataname数据库名,$ip链接数据库地址，$dbname链接数据库名，$dbpassword链接数据库密码
function database($datename,$ip='117.48.206.138',$dbname='root',$dbpassword='GMX1998')
{
    //$datename = "a3";

    $con = @mysql_connect($ip, $dbname,$dbpassword);
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }

    if (@mysql_query("CREATE DATABASE " . $datename, $con)) {
//        echo "Database.class created";
    } else {
        echo "Error creating database: " . mysql_error();
    }

    @mysql_select_db($datename, $con) or die("<meta charset='utf-8'>无法打开数据库");

    $sql = file_get_contents("Application/Admin/Common/cbg.sql"); //把SQL语句以字符串读入$sql
    $a = explode(";", $sql); //用explode()函数把‍$sql字符串以“;”分割为数组

    foreach ($a as $b) { //遍历数组
        $c = $b . ";"; //分割后是没有“;”的，因为SQL语句以“;”结束，所以在执行SQL前把它加上
        @mysql_query($c); //执行SQL语句
    }

    @mysql_close($con);
}

//返回格式化时间
function retime($time)
{
    return date("Y-m-d H:i:s", $time);
}

//检查文件类型
function file_type($filename)
{
    $file = fopen($filename, "rb");
    $bin = fread($file, 2); //只读2字节
    fclose($file);
    $strInfo = @unpack("C2chars", $bin);
    $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
    $fileType = '';
    switch ($typeCode)
    {
        case 7790:
            $fileType = 'exe';
            break;
        case 7784:
            $fileType = 'midi';
            break;
        case 8297:
            $fileType = 'rar';
            break;
        case 8075:
            $fileType = 'zip';
            break;
        case 255216:
            $fileType = 'jpg';
            break;
        case 7173:
            $fileType = 'gif';
            break;
        case 6677:
            $fileType = 'bmp';
            break;
        case 13780:
            $fileType = 'png';
            break;
        default:
            $fileType = 'unknown: '.$typeCode;
    }

    //Fix
    if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg';
    if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png';

    return $fileType;
}
